#include "gd32l23x.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "cpuusage.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

#include "DevicesDelay.h"
#include "DevicesADC.h"
#include "DevicesKey.h"
#include "DevicesLed.h"
#include "DevicesPower.h"
#include "DevicesPWM.h"
#include "DevicesQueue.h"
#include "DevicesRS485.h"
#include "DevicesSPI.h"
#include "DevicesSPIFlash.h"
#include "DevicesTimer.h"
#include "DevicesUart.h"
#include "DevicesWatchDog.h"
#include "DevicesSoftTimer.h"
#include "DevicesList.h"
#include "DevicesTime.h"
#include "DevicesRTC.h"
#include "DevicesWatchDog.h"
#include "DevicesUID.h"

#include "DriverDCDC.h"

#include "taskSoftTimer.h"
#include "taskMessageSlave.h"
#include "taskSystem.h"


TaskHandle_t g_TaskSystemInitHand = NULL;  /* 系统初始化任务句柄 */

/* SPI Flash 读、写互斥信号量 */
SemaphoreHandle_t g_xSpiFlashSemaphore;



void vApplicationIdleHook( void )
{
    /* CPU占用率计算 */
    vCPUUsageIdleHook();

    /* 喂狗 */
    vWatchdogReload();
}


void vUserSystemInit(void)
{

    enumQueueInit();

    cTimer8Init();

//    vLedInit();

    vKeyInit();

    vADCInit();

    vSPIFlashInit();

    cSoftTimerInit();

    vDCDCxInit();

    vWatchdogInit();
}

void vTaskSystemInit(void *pvParameters)
{
    printf("FreeRTOS start succeed...\r\n");

    /* 创建递归互斥信号量 */
    g_xSpiFlashSemaphore = xSemaphoreCreateRecursiveMutex();


    xTaskCreate(vTaskMessageSlave,      "Message Slave",    512,    NULL, configMAX_PRIORITIES - 3, &g_TaskMessageSlaveHand);


    vUserSystemInit();

    printf("SystemInit succeed.\r\n");


    /* 删除自己 */
    vTaskDelete( NULL );
}
